package com.zyf.wjy.dao;

import com.fasterxml.jackson.core.type.TypeReference;
import com.zyf.wjy.entity.Product;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 农产品DAO
 */
@Repository
public class ProductDao extends BaseDao<Product> {
    
    @Override
    protected String getFileName() {
        return "products.json";
    }
    
    @Override
    protected TypeReference<List<Product>> getTypeReference() {
        return new TypeReference<List<Product>>() {};
    }
    
    @Override
    protected Long getId(Product entity) {
        return entity.getId();
    }
    
    @Override
    protected void setId(Product entity, Long id) {
        entity.setId(id);
    }
    
    /**
     * 根据产品编号查询
     */
    public Product findByProductCode(String productCode) {
        return findAll().stream()
            .filter(product -> productCode.equals(product.getProductCode()))
            .findFirst()
            .orElse(null);
    }
    
    /**
     * 根据作物ID查询
     */
    public List<Product> findByCropId(Long cropId) {
        return findAll().stream()
            .filter(product -> cropId.equals(product.getCropId()))
            .toList();
    }
}

